/*
 * CadastrarContaReceberEmpresa.java
 *
 * Created on 10/03/2009, 14:21:12
 * Copyright Jaime Junior
 */
package Adecot.Financeiro;

import Adecot.*;
import Controller.BancoBO;
import Controller.ContaReceberEmpresaBO;
import Controller.EmpresaBO;
import Controller.Facade.IceDecorator;
import Controller.Facade.ValidaFacade;
import Controller.PedidoEmpresaBO;
import Model.ContaReceberEmpresa;
import Model.Empresa;
import Model.PedidoEmpresaVO;
import com.icesoft.faces.component.dragdrop.DragEvent;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.ext.HtmlSelectOneRadio;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectedData;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectionItems;
import com.icesoft.faces.component.jsfcl.data.PopupBean;
import com.icesoft.faces.component.jsfcl.data.SelectInputDateBean;
import com.icesoft.faces.component.panellayout.PanelLayout;
import com.icesoft.faces.component.panelpopup.PanelPopup;
import com.icesoft.faces.component.selectinputdate.SelectInputDate;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class CadastrarContaReceberEmpresa extends AbstractPageBean {
   // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

   private int __placeholder;

   /**
    * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
    * This method is automatically generated, so any user-specified code inserted
    * here is subject to being replaced.</p>
    */
   private void _init() throws Exception {
   }
   private DefaultSelectedData tipoSelecionado = new DefaultSelectedData();

   public DefaultSelectedData getTipoSelecionado() {
      return tipoSelecionado;
   }

   public void setTipoSelecionado(DefaultSelectedData dsd) {
      this.tipoSelecionado = dsd;
   }
   private DefaultSelectionItems tipos = new DefaultSelectionItems();

   public DefaultSelectionItems getTipos() {
      return tipos;
   }

   public void setTipos(DefaultSelectionItems dsi) {
      this.tipos = dsi;
   }
   private DefaultSelectedData bancoSelecionado = new DefaultSelectedData();

   public DefaultSelectedData getBancoSelecionado() {
      return bancoSelecionado;
   }

   public void setBancoSelecionado(DefaultSelectedData dsd) {
      this.bancoSelecionado = dsd;
   }
   private DefaultSelectionItems bancos = new DefaultSelectionItems();

   public DefaultSelectionItems getBancos() {
      return bancos;
   }

   public void setBancos(DefaultSelectionItems dsi) {
      this.bancos = dsi;
   }
   private HtmlSelectOneMenu comboBanco = new HtmlSelectOneMenu();

   public HtmlSelectOneMenu getComboBanco() {
      return comboBanco;
   }

   public void setComboBanco(HtmlSelectOneMenu hsom) {
      this.comboBanco = hsom;
   }
   private HtmlInputText campoConta = new HtmlInputText();

   public HtmlInputText getCampoConta() {
      return campoConta;
   }

   public void setCampoConta(HtmlInputText hit) {
      this.campoConta = hit;
   }
   private HtmlInputText campoNumeroCheque = new HtmlInputText();

   public HtmlInputText getCampoNumeroCheque() {
      return campoNumeroCheque;
   }

   public void setCampoNumeroCheque(HtmlInputText hit) {
      this.campoNumeroCheque = hit;
   }
   private HtmlInputText campoTitular = new HtmlInputText();

   public HtmlInputText getCampoTitular() {
      return campoTitular;
   }

   public void setCampoTitular(HtmlInputText hit) {
      this.campoTitular = hit;
   }
   private SelectInputDateBean dataVencimentoSelecionada = new SelectInputDateBean();

   public SelectInputDateBean getDataVencimentoSelecionada() {
      return dataVencimentoSelecionada;
   }

   public void setDataVencimentoSelecionada(SelectInputDateBean sidb) {
      this.dataVencimentoSelecionada = sidb;
   }
   private HtmlInputText campoValor = new HtmlInputText();

   public HtmlInputText getCampoValor() {
      return campoValor;
   }

   public void setCampoValor(HtmlInputText hit) {
      this.campoValor = hit;
   }
   private SelectInputDate campoDataVencimento = new SelectInputDate();

   public SelectInputDate getCampoDataVencimento() {
      return campoDataVencimento;
   }

   public void setCampoDataVencimento(SelectInputDate sid) {
      this.campoDataVencimento = sid;
   }
   private HtmlSelectOneRadio radioTipoPagamento = new HtmlSelectOneRadio();

   public HtmlSelectOneRadio getRadioTipoPagamento() {
      return radioTipoPagamento;
   }

   public void setRadioTipoPagamento(HtmlSelectOneRadio hsor) {
      this.radioTipoPagamento = hsor;
   }
   private HtmlInputText campoAgencia = new HtmlInputText();

   public HtmlInputText getCampoAgencia() {
      return campoAgencia;
   }

   public void setCampoAgencia(HtmlInputText hit) {
      this.campoAgencia = hit;
   }
   private SelectInputDateBean dataPagamentoSelecionada = new SelectInputDateBean();

   public SelectInputDateBean getDataPagamentoSelecionada() {
      return dataPagamentoSelecionada;
   }

   public void setDataPagamentoSelecionada(SelectInputDateBean sidb) {
      this.dataPagamentoSelecionada = sidb;
   }
   private HtmlOutputText labelBanco = new HtmlOutputText();

   public HtmlOutputText getLabelBanco() {
      return labelBanco;
   }

   public void setLabelBanco(HtmlOutputText hot) {
      this.labelBanco = hot;
   }
   private HtmlOutputText labelAgencia = new HtmlOutputText();

   public HtmlOutputText getLabelAgencia() {
      return labelAgencia;
   }

   public void setLabelAgencia(HtmlOutputText hot) {
      this.labelAgencia = hot;
   }
   private HtmlOutputText labelConta = new HtmlOutputText();

   public HtmlOutputText getLabelConta() {
      return labelConta;
   }

   public void setLabelConta(HtmlOutputText hot) {
      this.labelConta = hot;
   }
   private HtmlOutputText labelNumeroCheque = new HtmlOutputText();

   public HtmlOutputText getLabelNumeroCheque() {
      return labelNumeroCheque;
   }

   public void setLabelNumeroCheque(HtmlOutputText hot) {
      this.labelNumeroCheque = hot;
   }
   private HtmlOutputText labelTitular = new HtmlOutputText();

   public HtmlOutputText getLabelTitular() {
      return labelTitular;
   }

   public void setLabelTitular(HtmlOutputText hot) {
      this.labelTitular = hot;
   }
   private HtmlOutputText labelValor = new HtmlOutputText();

   public HtmlOutputText getLabelValor() {
      return labelValor;
   }

   public void setLabelValor(HtmlOutputText hot) {
      this.labelValor = hot;
   }
   private HtmlOutputText labelVencimento = new HtmlOutputText();

   public HtmlOutputText getLabelVencimento() {
      return labelVencimento;
   }

   public void setLabelVencimento(HtmlOutputText hot) {
      this.labelVencimento = hot;
   }
   private PanelLayout painelTeste = new PanelLayout();

   public PanelLayout getPainelTeste() {
      return painelTeste;
   }

   public void setPainelTeste(PanelLayout pl) {
      this.painelTeste = pl;
   }
   private DefaultSelectedData empresaSelecionada = new DefaultSelectedData();

   public DefaultSelectedData getEmpresaSelecionada() {
      return empresaSelecionada;
   }

   public void setEmpresaSelecionada(DefaultSelectedData dsd) {
      this.empresaSelecionada = dsd;
   }
   private DefaultSelectionItems empresas = new DefaultSelectionItems();

   public DefaultSelectionItems getEmpresas() {
      return empresas;
   }

   public void setEmpresas(DefaultSelectionItems dsi) {
      this.empresas = dsi;
   }
   private HtmlSelectOneMenu comboEmpresa = new HtmlSelectOneMenu();

   public HtmlSelectOneMenu getComboEmpresa() {
      return comboEmpresa;
   }

   public void setComboEmpresa(HtmlSelectOneMenu hsom) {
      this.comboEmpresa = hsom;
   }
   private SelectInputDate campoDataPagamento = new SelectInputDate();

   public SelectInputDate getCampoDataPagamento() {
      return campoDataPagamento;
   }

   public void setCampoDataPagamento(SelectInputDate sid) {
      this.campoDataPagamento = sid;
   }
   private HtmlInputText campoValorPagamento = new HtmlInputText();

   public HtmlInputText getCampoValorPagamento() {
      return campoValorPagamento;
   }

   public void setCampoValorPagamento(HtmlInputText hit) {
      this.campoValorPagamento = hit;
   }
   private PopupBean painelPopuSair = new PopupBean();

   public PopupBean getPainelPopuSair() {
      return painelPopuSair;
   }

   public void setPainelPopuSair(PopupBean pb) {
      this.painelPopuSair = pb;
   }
   private PanelPopup painelSair = new PanelPopup();

   public PanelPopup getPainelSair() {
      return painelSair;
   }

   public void setPainelSair(PanelPopup pp) {
      this.painelSair = pp;
   }
   private PopupBean painelPopupConsulta = new PopupBean();

   public PopupBean getPainelPopupConsulta() {
      return painelPopupConsulta;
   }

   public void setPainelPopupConsulta(PopupBean pb) {
      this.painelPopupConsulta = pb;
   }
   private PanelPopup painelConsulta = new PanelPopup();

   public PanelPopup getPainelConsulta() {
      return painelConsulta;
   }

   public void setPainelConsulta(PanelPopup pp) {
      this.painelConsulta = pp;
   }
   private HtmlCommandButton botaoSairSim = new HtmlCommandButton();

   public HtmlCommandButton getBotaoSairSim() {
      return botaoSairSim;
   }

   public void setBotaoSairSim(HtmlCommandButton hcb) {
      this.botaoSairSim = hcb;
   }
   private HtmlCommandButton botaoSairNao = new HtmlCommandButton();

   public HtmlCommandButton getBotaoSairNao() {
      return botaoSairNao;
   }

   public void setBotaoSairNao(HtmlCommandButton hcb) {
      this.botaoSairNao = hcb;
   }
   private HtmlCommandButton botaoConsultaOk = new HtmlCommandButton();

   public HtmlCommandButton getBotaoConsultaOk() {
      return botaoConsultaOk;
   }

   public void setBotaoConsultaOk(HtmlCommandButton hcb) {
      this.botaoConsultaOk = hcb;
   }
   private HtmlCommandButton botaoConsultaCancelar = new HtmlCommandButton();

   public HtmlCommandButton getBotaoConsultaCancelar() {
      return botaoConsultaCancelar;
   }

   public void setBotaoConsultaCancelar(HtmlCommandButton hcb) {
      this.botaoConsultaCancelar = hcb;
   }
   private PopupBean painelPopupAviso = new PopupBean();

   public PopupBean getPainelPopupAviso() {
      return painelPopupAviso;
   }

   public void setPainelPopupAviso(PopupBean pb) {
      this.painelPopupAviso = pb;
   }
   private PanelPopup painelAviso = new PanelPopup();

   public PanelPopup getPainelAviso() {
      return painelAviso;
   }

   public void setPainelAviso(PanelPopup pp) {
      this.painelAviso = pp;
   }
   private HtmlOutputText msgAvisoResultado = new HtmlOutputText();

   public HtmlOutputText getMsgAvisoResultado() {
      return msgAvisoResultado;
   }

   public void setMsgAvisoResultado(HtmlOutputText hot) {
      this.msgAvisoResultado = hot;
   }
   private HtmlOutputText msgAvisoPergunta = new HtmlOutputText();

   public HtmlOutputText getMsgAvisoPergunta() {
      return msgAvisoPergunta;
   }

   public void setMsgAvisoPergunta(HtmlOutputText hot) {
      this.msgAvisoPergunta = hot;
   }
   private HtmlCommandButton botaoAvisoSim = new HtmlCommandButton();

   public HtmlCommandButton getBotaoAvisoSim() {
      return botaoAvisoSim;
   }

   public void setBotaoAvisoSim(HtmlCommandButton hcb) {
      this.botaoAvisoSim = hcb;
   }
   private HtmlCommandButton botaoNaoAviso = new HtmlCommandButton();

   public HtmlCommandButton getBotaoNaoAviso() {
      return botaoNaoAviso;
   }

   public void setBotaoNaoAviso(HtmlCommandButton hcb) {
      this.botaoNaoAviso = hcb;
   }
   private PopupBean painelPopupMensagem = new PopupBean();

   public PopupBean getPainelPopupMensagem() {
      return painelPopupMensagem;
   }

   public void setPainelPopupMensagem(PopupBean pb) {
      this.painelPopupMensagem = pb;
   }
   private PanelPopup painelMensagem = new PanelPopup();

   public PanelPopup getPainelMensagem() {
      return painelMensagem;
   }

   public void setPainelMensagem(PanelPopup pp) {
      this.painelMensagem = pp;
   }
   private HtmlOutputText msgMensagemResultado = new HtmlOutputText();

   public HtmlOutputText getMsgMensagemResultado() {
      return msgMensagemResultado;
   }

   public void setMsgMensagemResultado(HtmlOutputText hot) {
      this.msgMensagemResultado = hot;
   }
   private HtmlCommandButton botaoMensagemOk = new HtmlCommandButton();

   public HtmlCommandButton getBotaoMensagemOk() {
      return botaoMensagemOk;
   }

   public void setBotaoMensagemOk(HtmlCommandButton hcb) {
      this.botaoMensagemOk = hcb;
   }
   private HtmlCommandButton botaoLimpar = new HtmlCommandButton();

   public HtmlCommandButton getBotaoLimpar() {
      return botaoLimpar;
   }

   public void setBotaoLimpar(HtmlCommandButton hcb) {
      this.botaoLimpar = hcb;
   }
   private HtmlCommandButton botaoNovo = new HtmlCommandButton();

   public HtmlCommandButton getBotaoNovo() {
      return botaoNovo;
   }

   public void setBotaoNovo(HtmlCommandButton hcb) {
      this.botaoNovo = hcb;
   }
   private HtmlCommandButton botaoConsultar = new HtmlCommandButton();

   public HtmlCommandButton getBotaoConsultar() {
      return botaoConsultar;
   }

   public void setBotaoConsultar(HtmlCommandButton hcb) {
      this.botaoConsultar = hcb;
   }
   private HtmlCommandButton botaoAlterar = new HtmlCommandButton();

   public HtmlCommandButton getBotaoAlterar() {
      return botaoAlterar;
   }

   public void setBotaoAlterar(HtmlCommandButton hcb) {
      this.botaoAlterar = hcb;
   }
   private HtmlCommandButton botaoImprimir = new HtmlCommandButton();

   public HtmlCommandButton getBotaoImprimir() {
      return botaoImprimir;
   }

   public void setBotaoImprimir(HtmlCommandButton hcb) {
      this.botaoImprimir = hcb;
   }
   private HtmlCommandButton botaoCadastrar1 = new HtmlCommandButton();

   public HtmlCommandButton getBotaoCadastrar1() {
      return botaoCadastrar1;
   }

   public void setBotaoCadastrar1(HtmlCommandButton hcb) {
      this.botaoCadastrar1 = hcb;
   }
   private HtmlInputText campoValorReceber = new HtmlInputText();

   public HtmlInputText getCampoValorReceber() {
      return campoValorReceber;
   }

   public void setCampoValorReceber(HtmlInputText hit) {
      this.campoValorReceber = hit;
   }

   // </editor-fold>
   /**
    * <p>Construct a new Page bean instance.</p>
    */
   public CadastrarContaReceberEmpresa() {
   }

   /**
    * <p>Callback method that is called whenever a page is navigated to,
    * either directly via a URL, or indirectly via page navigation.
    * Customize this method to acquire resources that will be needed
    * for event handlers and lifecycle methods, whether or not this
    * page is performing post back processing.</p>
    *
    * <p>Note that, if the current request is a postback, the property
    * values of the components do <strong>not</strong> represent any
    * values submitted with this request.  Instead, they represent the
    * property values that were saved for this view when it was rendered.</p>
    */
   @Override
   public void init() {
      // Perform initializations inherited from our superclass
      super.init();

      // Perform application initialization that must complete
      // *before* managed components are initialized
      // TODO - add your own initialiation code here

      // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
      // Initialize automatically managed components
      // *Note* - this logic should NOT be modified
      try {
         _init();
      } catch (Exception e) {
         log("CadastrarContaReceberEmpresa Initialization Failure", e);
         throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
      }

      tipos.clear();
      tipos.add(new SelectItem("Cheque", "Cheque"));
      tipos.add(new SelectItem("Dinheiro", "Dinheiro"));
      radioTipoPagamento.setValue("Dinheiro");
      setBancos(new IceDecorator(BancoBO.class).montaCombo());
      setEmpresas(new IceDecorator(EmpresaBO.class).montaCombo());

      limparCampos();
//      comboBanco.setRendered(false);
//      campoAgencia.setRendered(false);
//      campoConta.setRendered(false);
//      campoNumeroCheque.setRendered(false);
//      campoTitular.setRendered(false);
//      campoValor.setRendered(false);
//      campoDataVencimento.setRendered(false);

      painelTeste.setRendered(false);

      painelSair.setRendered(false);
      painelConsulta.setRendered(false);
      painelAviso.setRendered(false);
      painelMensagem.setRendered(false);
   // </editor-fold>
   // Perform application initialization that must complete
   // *after* managed components are initialized
   // TODO - add your own initialization code here
   }

   /**
    * <p>Callback method that is called after the component tree has been
    * restored, but before any event processing takes place.  This method
    * will <strong>only</strong> be called on a postback request that
    * is processing a form submit.  Customize this method to allocate
    * resources that will be required in your event handlers.</p>
    */
   @Override
   public void preprocess() {
   }

   /**
    * <p>Callback method that is called just before rendering takes place.
    * This method will <strong>only</strong> be called for the page that
    * will actually be rendered (and not, for example, on a page that
    * handled a postback and then navigated to a different page).  Customize
    * this method to allocate resources that will be required for rendering
    * this page.</p>
    */
   @Override
   public void prerender() {
   }

   /**
    * <p>Callback method that is called after rendering is completed for
    * this request, if <code>init()</code> was called (regardless of whether
    * or not this was the page that was actually rendered).  Customize this
    * method to release resources acquired in the <code>init()</code>,
    * <code>preprocess()</code>, or <code>prerender()</code> methods (or
    * acquired during execution of an event handler).</p>
    */
   @Override
   public void destroy() {
   }

   /**
    * <p>Retornar uma referência para o Bean de dados limitados.</p>
    *
    * @retorna uma referência para o bean de dados limitados
    */
   protected ApplicationBean1 getApplicationBean1() {
      return (ApplicationBean1) getBean("ApplicationBean1");
   }

   /**
    * <p>Retornar uma referência para o Bean de dados limitados.</p>
    *
    * @retorna uma referência para o bean de dados limitados
    */
   protected SessionBean1 getSessionBean1() {
      return (SessionBean1) getBean("SessionBean1");
   }

   /**
    * <p>Retornar uma referência para o Bean de dados limitados.</p>
    *
    * @retorna uma referência para o bean de dados limitados
    */
   protected RequestBean1 getRequestBean1() {
      return (RequestBean1) getBean("RequestBean1");
   }

   public void radioTipoPagamento_processValueChange(ValueChangeEvent vce) {
      String opcao = radioTipoPagamento.getValue().toString();


      if (opcao.equals("Cheque")) {
         painelTeste.setRendered(true);
      } else {
         painelTeste.setRendered(false);
      }
   }

   public String botaoSairSim_action() {
      //return null means stay on the same page
      return "menu";
   }

   public String botaoSairNao_action() {
      painelSair.setRendered(false);
      return null;
   }

   public String botaoSair_action() {
      painelSair.setRendered(true);
      return null;
   }

   public String botaoConsultaOk_action() {
      painelAviso.setRendered(true);
      return null;
   }

   public String botaoConsultaCancelar_action() {
      painelConsulta.setRendered(false);
      return null;
   }

   public String botaoConsultar_action() {
      painelConsulta.setRendered(true);
      return null;
   }

   public String botaoAvisoSim_action() {
      painelAviso.setRendered(false);
      return null;
   }

   public String botaoNaoAviso_action() {
      painelAviso.setRendered(false);
      painelConsulta.setRendered(false);
      return null;
   }

   public void limparCampos() {
      FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove("ContaEmpresa");
      painelTeste.setRendered(false);
      campoAgencia.resetValue();
      campoConta.resetValue();
      campoDataPagamento.resetValue();
      campoDataVencimento.resetValue();
      campoNumeroCheque.resetValue();
      campoTitular.resetValue();
      campoValor.resetValue();
      campoValorPagamento.resetValue();
      SelectItem emp = (SelectItem) empresas.get(0);
      comboEmpresa.setValue(emp.getValue());
      SelectItem banc = (SelectItem) bancos.get(0);
      comboBanco.setValue(banc.getValue());
      selecionaEmpresa();

   }

   public String botaoNovo_action() {
      limparCampos();
      return null;
   }

   public String botaoCadastrar1_action() {
      ContaReceberEmpresa conta = (ContaReceberEmpresa) getBean("ContaEmpresa");
//      conta.setDataPagamento(new ValidaFacade().consertarData(conta.getDataPagamento()));
//      conta.setDataVencimento(new ValidaFacade().consertarData(conta.getDataVencimento()));
//      conta.getEmpresa().setCodEmpresa(Integer.parseInt(comboEmpresa.getValue().toString()));
//      conta.getBanco().setCodBanco(Integer.parseInt(comboBanco.getValue().toString()));
      try {
         new IceDecorator(ContaReceberEmpresaBO.class).salvar(conta);
         limparCampos();
         msgAvisoResultado.setValue("Operação Efetuada Com Sucesso");
         painelMensagem.setRendered(true);

      } catch (Exception ex) {
         ex.printStackTrace();
         msgAvisoResultado.setValue("Erro Ao Efetuar Operação");
         painelMensagem.setRendered(true);
      }
      return null;
   }

   public void comboEmpresa_processValueChange(ValueChangeEvent vce) {
      if (vce.getComponent().getId().equals(comboEmpresa.getId())) {
         FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove("ContaEmpresa");
         FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove("Empresa");
         selecionaEmpresa();
      }
   }

   public void selecionaEmpresa() {
      try {
         Empresa empresa = (Empresa) new IceDecorator(EmpresaBO.class).retornaObjetoCodigo("codEmpresa",
                 Integer.parseInt(comboEmpresa.getValue().toString()));

         setBean("Empresa", empresa);
      } catch (Exception ex) {
      }
   }

   public String botaoMensagemOk_action() {
      painelMensagem.setRendered(false);
      return null;
   }

   public void campoValorPagamento_processValueChange(ValueChangeEvent vce) {
      selecionaEmpresa();
      Empresa empresa = (Empresa) getBean("Empresa");
      ContaReceberEmpresa conta = (ContaReceberEmpresa) getBean("ContaEmpresa");
      setBean("Empresa", empresa);
   }
}

